package cz.cvut.fel.wavrecordtest;


public class simpleMath{
	public static double[] correlation(short[] data_in) {
		  int length;
		  length = data_in.length;
		  double[] data = null;
		  double[] corr = null;
		  int shift;		
		  double R = 0;
		  
		  data = new double[3*length-2];
		  corr = new double[2*length];
		  
		  for(int i = 0; i<3*length-2;i++){
			  if(i<length||i>2*length-1){
				  data[i] = 0;
			  } else {
				  data[i] = data_in[i-length];
			  }
		  }
		  
		  for(int i = 0; i<2*length; i++){	  
			  corr[i] = 0;
		  }
		  
		  for (shift = 0; shift<2*length-1; shift++){
		    for(int j = 0; j<length;j++){
		      if (data[j+shift]==0 || data_in[j]==0) 
		    	  continue; 
		      else
		      R += data[j+shift]*data_in[j];
		    }
		    corr[shift] = R;
		    R = 0;
		  }						
		return corr;
	}
	
	public static double[] butterworth(double[] data) {
		int order;
		int length;
		double[] y;
		double[] x;
		double[] b =  {0.0013,    0.0051,    0.0076,    0.0051,    0.0013};
		double[] a =  {1.0000,   -2.8869,    3.2397,   -1.6565,    0.3240};			
		
		order = a.length;		
		length = data.length;
				
		x = new double[length];
		y = new double[length];
		x = data;
		
		if (order!= 5) return null;
		
		for(int i = 0; i<length; i++){
			y[i] = 0;
		}
				
		// from MATLAB a =  {1.0000,   -2.8869,    3.2397,   -1.6565,    0.3240};
		// from MATLAB b =  {0.0013,    0.0051,    0.0076,    0.0051,    0.0013};
		for(int i = order; i<length; i++){
			y[i] = b[0]*x[i] + b[1]*x[i-1] + b[2]*x[i-2] + b[3]*x[i-3] + b[4]*x[i-4]
					 - a[1]*y[i-1] - a[2]*y[i-2] - a[3]*y[i-3] - a[4]*y[i-4];
		}
		
		return y;
	}	
	
	public static double[] butterworth(short[] data) {
		int order;
		int length;
		double[] y;
		short[] x;
		double[] b =  {0.0013,    0.0051,    0.0076,    0.0051,    0.0013};
		double[] a =  {1.0000,   -2.8869,    3.2397,   -1.6565,    0.3240};			
		
		order = a.length;		
		length = data.length;
				
		x = new short[length];
		y = new double[length];
		x = data;
		
		if (order!= 5) return null;
		
		for(int i = 0; i<length; i++){
			y[i] = 0;
		}
				
		// from MATLAB a =  {1.0000,   -2.8869,    3.2397,   -1.6565,    0.3240};
		// from MATLAB b =  {0.0013,    0.0051,    0.0076,    0.0051,    0.0013};
		for(int i = order; i<length; i++){
			y[i] = b[0]*x[i] + b[1]*x[i-1] + b[2]*x[i-2] + b[3]*x[i-3] + b[4]*x[i-4]
					 - a[1]*y[i-1] - a[2]*y[i-2] - a[3]*y[i-3] - a[4]*y[i-4];
		}
		
		return y;
	}
  
  public static double[] abs(double[] x){
    for(int i = 0; i<x.length; i++){
      x[i] = Math.sqrt(x[i]*x[i]); 
    }
    return x;
  }
	
  public static double[] diff(double[] x){
    double[] x_diff;
    x_diff = new double[x.length-1];
	  for(int i = 1;i<x.length; i++){
      x_diff[i-1] = x[i] - x[i-1];
    }
	return x_diff;
  }
  
	public static int localMax(double[] data){
		double max;
		int index;
		
		max = data[0];
		index = 0;
		for (int i = 0; i < data.length; i++) {
			if (max<data[i]) {
				max = data[i];
				index = i;
			} 
		}
		return index;
	}
	
	public static int localMin(double[] data){
		double min;
		int index;
		
		min = data[0];
		index = 0;
		for (int i = 0; i < data.length; i++) {
			if (min<data[i]) {
				min = data[i];
				index = i;
			} 
		}
		return index;		
	}
	
	public static double[] getSubsequent(int start, int end, double[] data) {
		double[] output;
		output = new double[end-start+1];
			for (int i = start; i < end+1; i++) {
				output[i-start] = data[i];				
			}
		return output;
	}
	
	public static short[] getSubsequent(int start, int end, short[] data) {
		short[] output;
		output = new short[end-start+1];
			for (int i = start; i < end+1; i++) {
				output[i-start] = data[i];				
			}
		return output;
	}	

}